import React from 'react';
import { Table, Button, Space, Tag, Popconfirm, TablePaginationConfig } from 'antd';
import { EditOutlined, DeleteOutlined } from '@ant-design/icons';
import { Role } from '@/pages/Settings/RoleManagement/types';

interface RoleTableProps {
  roles: Role[];
  loading: boolean;
  pagination?: {
    page: number;
    limit: number;
    total: number;
  };
  onChange?: (pagination: TablePaginationConfig) => void;
  onEdit: (role: Role) => void;
  onDelete: (id: number) => void;
}

const RoleTable: React.FC<RoleTableProps> = ({ roles, loading, pagination, onChange, onEdit, onDelete }) => {
  const columns = [
    {
      title: '角色名称',
      dataIndex: 'name',
      key: 'name',
    },
    {
      title: '角色编码',
      dataIndex: 'code',
      key: 'code',
    },
    {
      title: '描述',
      dataIndex: 'description',
      key: 'description',
      ellipsis: true,
    },
    {
      title: '权限',
      dataIndex: 'permissions',
      key: 'permissions',
      render: (permissions: string[]) => (
        <div>
          {permissions && permissions.map(permission => (
            <span key={permission} style={{ marginRight: 8, color: '#1890ff' }}>
              {permission}
            </span>
          ))}
        </div>
      ),
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
    },
    {
      title: '操作',
      key: 'actions',
      render: (_: any, record: Role) => (
        <Space>
          <Button
            type="link"
            icon={<EditOutlined />}
            onClick={() => onEdit(record)}
          >
            编辑
          </Button>
          <Popconfirm
            title="确定要删除这个角色吗？"
            onConfirm={() => onDelete(record.id)}
            okText="确定"
            cancelText="取消"
          >
            <Button type="link" danger icon={<DeleteOutlined />}>删除</Button>
          </Popconfirm>
        </Space>
      ),
    },
  ];

  return (
    <Table
      columns={columns}
      dataSource={roles}
      loading={loading}
      rowKey="id"
      pagination={pagination ? {
        current: pagination.page,
        pageSize: pagination.limit,
        total: pagination.total,
        showSizeChanger: true,
        showQuickJumper: true,
        showTotal: (total) => `共 ${total} 条记录`,
      } : false}
      onChange={onChange}
    />
  );
};

export default RoleTable; 